home *** CD-ROM | disk | FTP | other *** search
- Path: god.bel.alcatel.be!usenet
- From: bailleuf@btmaa.bel.alcatel.be (Frank Bailleul)
- Newsgroups: comp.lang.c++
- Subject: Re: template classes in dll? (VC++ 4.0)
- Date: Fri, 29 Mar 1996 16:46:37 GMT
- Organization: Alcatel Bell
- Message-ID: <4jh3u5$ek5@btmpjg.god.bel.alcatel.be>
- References: <1996Mar6.133456.3709@cs.mun.ca> <313E48F6.41C67EA6@sparky.hampshire.ma.us> <4iim7m$ime@news2.ios.com> <3152CB80.4106@datalytics.com>
- NNTP-Posting-Host: 138.203.28.170
- X-Newsreader: Forte Free Agent 1.0.82
-
- Rob Stewart <stew@datalytics.com> wrote:
-
- >Vlastimil Adamovsky wrote:
- >>
- >> Scott Reed <scott@sparky.hampshire.ma.us> wrote:
- >>
- >> >I'm having difficulty building a VC++ 4.0 dll with template classes.
- >> >The way I'm using the DllExport spec (actually __declspec(dllexport))
- >> >doesn't appear to fit the syntax for template classes, i.e.
- >> > template <class T> class DllExport ListOf {...};
- >> >results in a syntax error ("error C2960: template 'class' requires a
- >> >tag-name"). I've searched the online help for hours but I can't figure
- >> >this out. I'd really appreciate any help on this.
- >> > - scott
- >>
- >> If you really insist on using templates, then try to instantaite them
- >> in the your DLL and then you should not get any problems using it.
- >>
-
- >To instantiate a template requires the complete source code.
- >Thus, if a user of your DLL needs a specialization that is not
- >otherwise present in your DLL, they will require the source
- >code; the source is not exported.
-
- >If you want to make specific instantiations part of your DLL,
- >then you'll need to instantiate the template for specific
- >template parameters and export them. I'm not certain how to do
- >that in VC, but there is probably help or a KB article
- >explaining how to do it. Even doing this, someone may wish to
- >instantiate your template for some other template parameters.
- >Therefore, you'll need to provide the complete source code
- >anyway.
-
- >If you don't intend to make specific instantiations part of your
- >DLL, then there is nothing to export. You just have to
- >distribute the complete template source code with your DLL.
-
- >Either way, the source code is visible--the one drawback to
- >templates.
-
- >--
- >Robert Stewart | My opinions are usually my own.
- >Datalytics, Inc. | stew@datalytics.com
-
- Hello,
-
- I've been experimenting with template classes in DLLs in Visual C++
- 4.0.
-
- What I wanted to do is an export of an instantiated STL container
- template class : std::vector<MyClass> where MyClass is an arbitrary
- class.
- I did that by explicitely instantiating the container template class
- in the header file for the DLL :
-
- template std::vector<MyClass>;
-
- and by using an extern template class in the header file of the client
- which uses the DLL :
-
- extern template std::vector<MyClass>;
- // the extern keyword is a VC++ language extension
-
- Now in the DLL I have the function :
-
- std::vector<MyClass> GetList();
-
- which passes a container to the client.
-
- In the client I write :
-
- std::vector<MyClass> v = GetList();
-
- This performs the following actions :
-
- [1] call a constructor for v;
- [2] call a constructor in GetList() for the return value;
- [3] assign the return value of GetList() to v;
- [4] call the destructor for the return value;
-
- The client crashes at [4].
-
- Why ?
-
- template<class T> class vector
- has a static member : allocator<T> static_allocator.
- Thus, in the DLL module there exists a static allocator object (call
- it static1) and in the client exists another static allocator object
- (call it static2).
-
- [2] is constructed with allocator static1
- [4] is deleted with allocator static2.
-
- The solution is obvious : delete in [4] with allocator static1.
- The problem is : how ?
- 1. Rewrite the STL code and don't use statics
- 2. Don't work with return values but instead write :
- void GetList( std::vector<MyClass>& );
-
- 2 will work, but then I can't use functions.
- 1 is something I don't want to do.
-
- So, my question is basicly : how can I export a static ?
-
- Thanks in advance for feedback,
-
- Frank.
-
-